<!-- 具体页面结构 -->
<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>{{ company.name }}</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addFn('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template slot-scope="scope">
            <el-row type="flex" justify="space-between" align="middle" style="height: 40px; width: 100%;">
              <el-col :span="20">
                <svg-icon v-if="scope.data.children.length !== 0" :icon-class="scope.node.expanded ? 'sub' : 'add'" />
                <svg-icon v-else icon-class="angel" />
                <span @click="text(scope)">{{ scope.data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ scope.data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addFn(scope.data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editFn(scope.data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item
                          v-if="scope.data.children.length === 0"
                          @click.native="deleteFn(scope.data.id)"
                          >删除部门
                        </el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '编辑' : '添加'"
      :visible.sync="centerDialogVisible"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <DeptDialog
        v-if="centerDialogVisible"
        :id="cid"
        :list="this.originList"
        ref="dialog"
        :is-edit="isEdit"
        @success="closeAdd"
        @close="centerDialogVisible = false"
      />
    </el-dialog>
  </div>
</template>
<script>
import { getDepartmentsAPI, delDepartmentsAPI } from '@/api/departments'
import { tranListToTreeData } from '@/utils/tranListToTreeData'
import DeptDialog from './component/deptDialog.vue'
export default {
  components: {
    DeptDialog
  },
  data() {
    return {
      // 依赖一份树形数据
      isEdit: false,
      list: [],
      originList: [],
      centerDialogVisible: false,
      cid: '',
      company: {
        // 公司信息
        name: '',
        id: ''
      }
    }
  },
  created() {
    this.getDepartments()
  },
  methods: {
    text(val) {
      console.log(val)
    },
    async delDepartments(id) {
      const res = await delDepartmentsAPI(id)
      this.getDepartments()
      console.log(res)
    },
    deleteFn(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.delDepartments(id)
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    closeAdd() {
      this.centerDialogVisible = false
      this.getDepartments()
    },
    addFn(id) {
      this.cid = id
      console.log(this.cid)
      this.centerDialogVisible = true
      this.isEdit = false
    },
    editFn(id) {
      this.cid = id

      console.log(this.cid)
      this.centerDialogVisible = true
      this.isEdit = true
      this.$nextTick(() => {
        this.$refs.dialog.getDepartDetail()
      })
    },
    async getDepartments() {
      const res = await getDepartmentsAPI()

      this.company.id = res.data.companyId
      this.company.name = res.data.companyName
      res.data.depts.shift()
      this.originList = res.data.depts.map(({ id, pid, code, name }) => {
        return { id, name, code, pid }
      })
      this.list = tranListToTreeData(res.data.depts)
      console.log(this.list)
    }
  }
}
</script>
